package forum.dao.file;

import forum.entities.Category;
import forum.dao.CategoryDAO;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.math.BigInteger;
import java.util.List;

public class FileCategoryDAO extends FileGenericDAO<Category> implements CategoryDAO {

    public FileCategoryDAO() {
        super("src\\main\\resources\\categories.xml","categories","category");
    }

    @Override
    protected Element mapEntityToElement(Document document, Element element, Category category) {
        Element categoryNode = document.createElement("category");
        element.appendChild(categoryNode);

        Node idNode = document.createElement("id");
        idNode.setTextContent(category.getId().toString());
        categoryNode.appendChild(idNode);

        Node categoryNameNode = document.createElement("categoryName");
        categoryNameNode.setTextContent(category.getCategoryName());
        categoryNode.appendChild(categoryNameNode);

        Node parentCategoryIdNode = document.createElement("parentCategoryName");
        parentCategoryIdNode.setTextContent(category.getParentCategoryId().toString());
        categoryNode.appendChild(parentCategoryIdNode);

        Node authorIdNode = document.createElement("authorId");
        authorIdNode.setTextContent(category.getAuthorId().toString());
        categoryNode.appendChild(authorIdNode);

        return categoryNode;
    }

    @Override
    protected Category mapElementToEntity(Document document, Element categoryNode) {
        NodeList childNodes = categoryNode.getChildNodes();
        Category category = new Category(new BigInteger(childNodes.item(0).getTextContent()));
        category.setCategoryName(childNodes.item(1).getTextContent());
        category.setParentCategoryId(new BigInteger(childNodes.item(2).getTextContent()));
        category.setAuthorId(new BigInteger(childNodes.item(3).getTextContent()));
        return category;
    }

    @Override
    public List<Category> findCategoriesByParentCategoryId(BigInteger parentCategoryId) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    protected Category newEntity() {
        return new Category(getNewKey());
    }

    @Override
    public Category getRootCategory() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }
}
